<?php

/**
 * accounts actions.
 *
 * @package    corpon
 * @subpackage accounts
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class accountsActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->accounts = new Account();

    $this->pager = new sfDoctrinePager('Account',sfConfig::get('app_max_accounts'));
    $this->pager->setQuery($this->accounts->getAccountsQuery());
    $this->pager->setPage($request->getParameter('page', 1));
    $this->pager->init();

    $this->accounts = $this->pager->getResults();
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->account = Doctrine_Core::getTable('Account')->find(array($request->getParameter('id_account')));
    //$this->account = $this->getRoute()->getObject();
    $this->forward404Unless($this->account);
  }

  public function executeProducts(sfWebRequest $request)
  {
    $this->account = Doctrine_Core::getTable('Account')->findOneByCompanyNameUrl(array($request->getParameter('companyNameUrl')));
    //$this->account = $this->getRoute()->getObject();

    $this->forward404Unless($this->account);
    
    $this->products = new Product();
    
    $this->idaccount = $this->account->getIdAccount();
    
    $this->productss = $this->products->getProductsQueryAccount($this->account->getIdAccount());
    
    $today = date('Y-m-d');
    
    $this->productHigthlight = Doctrine::getTable('Product')
        ->createQuery('p')
        ->select('p.*')
        ->where('p.status = 1')
        ->andWhere('p.highlight = 1')
        ->andWhere('p.start_date <= ?',$today)
        ->andWhere('p.end_date >= ?',$today)
        ->andWhere('p.id_account = ?',$this->account->getIdAccount())
        ->orderBy('Rand()')
        ->limit('1')
        ->execute();
    $this->priceHigthlight = explode(".", $this->productHigthlight[0]->getDiscountedPrice());

    $this->pager = new sfDoctrinePager('Product',sfConfig::get('app_max_products'));
    $this->pager->setQuery($this->products->getProductsQuery($this->account->getIdAccount()));
    $this->pager->setPage($request->getParameter('page', 1));
    $this->pager->init();

    $this->products = $this->pager->getResults();
  }

  public function executeProductsList(sfWebRequest $request)
  {
    $this->account = Doctrine_Core::getTable('Account')->findOneByCompanyNameUrl(array($request->getParameter('companyNameUrl')));
    //$this->account = $this->getRoute()->getObject();
    $this->products = new Product();

    $this->pager = new sfDoctrinePager('Product',sfConfig::get('app_max_products'));
    $this->pager->setQuery($this->products->getProductsQuery($this->account->getIdAccount()));
    $this->pager->setPage($request->getParameter('page', 1));
    $this->pager->init();

    $this->products = $this->pager->getResults();

    $this->forward404Unless($this->account);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new AccountForm();
                    $today = date('Y-m-d');
      $this->products = Doctrine::getTable('Product')
                ->createQuery('p')
                ->select('p.*')
                ->where('p.status = 1')
                ->andWhere('p.start_date <= ?',$today)
                ->andWhere('p.end_date >= ?',$today)
                ->orderBy('Rand()')
                ->limit('4')
                ->execute();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new AccountForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($account = Doctrine_Core::getTable('Account')->find(array($request->getParameter('id_account'))), sprintf('Object account does not exist (%s).', $request->getParameter('id_account')));
    $this->form = new AccountForm($account);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($account = Doctrine_Core::getTable('Account')->find(array($request->getParameter('id_account'))), sprintf('Object account does not exist (%s).', $request->getParameter('id_account')));
    $this->form = new AccountForm($account);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($account = Doctrine_Core::getTable('Account')->find(array($request->getParameter('id_account'))), sprintf('Object account does not exist (%s).', $request->getParameter('id_account')));
    $account->delete();

    $this->redirect('accounts/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $account = $form->save();

      $filename = $account->getCompanyLogo();

      // IMAGEN GRANDE
      $img = new sfImage(sfConfig::get('sf_upload_dir').'/logos/'.$filename);
      $response = $this->getResponse();
      $response->setContentType($img->getMIMEType());

      $width = $img->getWidth();
      $height = $img->getHeight();
      $new_width = $width;
      $new_height = $height;
      while ($new_width >= 200 or $new_height >= 84)
      {
        $new_width = $new_width * 99/100;
        $new_height = $new_height * 99/100;
      }

      $img->resize($new_width,$new_height);
      
      $img->saveAs(sfConfig::get('sf_upload_dir').'/logos/'.$filename);

      $this->redirect('accounts/index');
    }
  }
}
